//
//  SelectionCell.m
//  diaoyuren
//
//  Created by 钓鱼人 on 15/12/18.
//  Copyright © 2015年 game. All rights reserved.
//

#import "SelectionCell.h"
#import "Masonry.h"
#import "UIImageView+WebCache.h"
#import "PageImgModel.h"
#import "ZanButton.h"
#import "AppDelegate.h"
#import "NSString+HtmlCharact.h"
#import "TYAttributedLabel.h"
#import "UILabel+BoundRect.h"
#import "UIButton+WebCache.h"
#define pointX 10

#define cellhightCount (SCREEN_HEIGHT*0.07*3+10)

#define titleHeight (SCREEN_HEIGHT*0.063)
@interface SelectionCell()

@property (weak, nonatomic) IBOutlet MBLabelWithFontAdapter *titleLab;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *tilelableHight;

@property (weak, nonatomic) IBOutlet UIImageView *userHeadImage;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *userheadWidth;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *userheadHeith;

@property (weak, nonatomic) IBOutlet MBLabelWithFontAdapter *userNameLab;
@property (weak, nonatomic) IBOutlet MBButtonWithFontAdapter *commentBtn;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *commentBtnWidth;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *commentBtnHight;

@property (weak, nonatomic) IBOutlet MBLabelWithFontAdapter *timeshowLab;
@property (weak, nonatomic) IBOutlet MBLabelWithFontAdapter *localshowLab;
@property (weak, nonatomic) IBOutlet UIImageView *localimage;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *localImageWidth;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *localImageHeight;

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *lin1Hight;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *lin2Width;
@property (weak, nonatomic) IBOutlet UILabel *cellFoot;

@property (nonatomic,strong) UIView *bacView;

@property (nonatomic,strong) PageImgModel *imgModel;

@property (nonatomic,strong) UIImageView *videoImage;
@property (nonatomic,strong) TYAttributedLabel *contentLab;
@property (nonatomic,strong) AppDelegate *app;

@end
@implementation SelectionCell

-(void)awakeFromNib{
    
     self.app = (AppDelegate *)[UIApplication sharedApplication].delegate;
    self.selectionStyle = 0;
    
    [self.praiseBtn setImage:[UIImage imageNamed:@"btn-reply-green"] forState:UIControlStateSelected | UIControlStateHighlighted];
    [self.praiseBtn setTitleColor:mainColor forState:UIControlStateSelected];
    [self.praiseBtn setTitleColor:mainColor forState:UIControlStateSelected | UIControlStateHighlighted];
    
    self.cellFoot.backgroundColor = tabGropBackColor;
  
    self.tilelableHight.constant = SCREEN_HEIGHT*0.063;
    self.bacView = [[UIView alloc]initWithFrame:CGRectMake(0, titleHeight, SCREEN_WIDTH, 0)];
    [self.contentView addSubview:self.bacView];
    
    
    self.userheadWidth.constant = SCREEN_WIDTH*0.06;
    self.userheadHeith.constant = self.userheadWidth.constant;
    radius(self.userHeadImage, self.userheadWidth.constant/2);
    
    self.localImageHeight.constant = 15;
    self.localImageWidth.constant = 15;
    
    self.lin1Hight.constant = 0.5;
    self.lin2Width.constant = 0.5;
    
    self.commentBtnWidth.constant = SCREEN_WIDTH/2-1;
    self.commentBtnHight.constant = SCREEN_HEIGHT*0.07;
    
    self.contentView.backgroundColor = [UIColor whiteColor];
    
    self.userNameLab.userInteractionEnabled = YES;
    self.userHeadImage.userInteractionEnabled = YES;
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(SpaceClick)];
    
    
    [self.userNameLab addGestureRecognizer:tap];
    
    UITapGestureRecognizer *Imgetap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(SpaceClick)];
    
    [self.userHeadImage addGestureRecognizer:Imgetap];
    
    [self.commentBtn addTarget:self action:@selector(ComentClick) forControlEvents:UIControlEventTouchUpInside];
    
    
}


-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        
    }
    return self;
}
-(void)setModel:(thread_listModel *)model{
    
    _model = model;
    
//
//    if (_model) {
//        
        self.titleLab.text = _model.subject;
        
        [self.bacView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
//
        if ([_model.style isEqualToString:@"text"]) {
            
            UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(10, 0, SCREEN_WIDTH-20, 0)];
            label.numberOfLines = 0;
            label.text = _model.message;
            label.font = font(SCREEN_WIDTH*0.05);
            
            [self.bacView addSubview:label];
        
            CGRect labrect = [label boundingRectWithInitSize:CGSizeMake(SCREEN_WIDTH-20, 0)];
            
            label.height = labrect.size.height;
            self.bacView.height = labrect.size.height;

        }else{
            
            self.bacView.height = SCREEN_WIDTH*0.3;
            NSInteger count = _model.images_list.count;
        
            if (count>2) {
                count=3;
            }
    
            for (int i=0; i<count; i++) {
                
                CGFloat xd = 5;
                CGFloat w = (screen_width-xd*(count-1)-pointX*2)/count;
                UIButton *imgeview =[UIButton buttonWithType:UIButtonTypeCustom];
                imgeview.frame = CGRectMake(10+i*(w+xd), 0, w, self.bacView.height);
    
                imgeview.enabled = NO;
                imgeview.adjustsImageWhenDisabled = NO;
                imgeview.adjustsImageWhenHighlighted = NO;
                imgeview.imageView.contentMode  = UIViewContentModeScaleAspectFill;
    
                PageImgModel *imagemodel = _model.images_list[i];
                
                [imgeview sd_setImageWithURL:[NSURL URLWithString:imagemodel.image_url] forState:UIControlStateNormal];
                
                [self.bacView addSubview:imgeview];
            }
            
            if ([_model.style isEqualToString:@"video"]&&count==1) {
                [self CreatVideoPlay];
            }
        }
//
        self.userNameLab.text = _model.author;
        
        [self.userHeadImage sd_setImageWithURL:[NSURL URLWithString:_model.avatar]];
        
        self.timeshowLab.text = [_model.dateline filterresult];
        
        [self.commentBtn setTitle:_model.replies forState:UIControlStateNormal];
        
        self.height = self.bacView.height+cellhightCount;
        
        self.tid = _model.tid;
        self.style  = _model.style;
    
    
    if (_model.is_support.integerValue==1) {
        [self.praiseBtn setTitle:_model.recommend_add forState:UIControlStateNormal];
        self.praiseBtn.selected = YES;
    }else{
    
        if (_model.isZan==YES) {
            NSInteger count = _model.recommend_add.integerValue+1;
            [self.praiseBtn setTitle:[NSString stringWithFormat:@"%ld",(long)count] forState:UIControlStateNormal];
            self.praiseBtn.selected = YES;
            
        }else{
            
            [self.praiseBtn setTitle:_model.recommend_add forState:UIControlStateNormal];
            self.praiseBtn.selected = NO;
        }
    
    }
    
        if (_model.location.length!=0) {
            self.localshowLab.text = _model.location;
            self.localimage.alpha = 1;
            self.localshowLab.alpha = 1;
            
        }else{
            
            self.localshowLab.alpha = 0;
            self.localimage.alpha = 0;
        }
//    }
}

/**
 *  显示播放图片
 */
-(void)CreatVideoPlay{
    [_videoImage removeFromSuperview];
    _videoImage = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 85, 85)];
    
    _videoImage.image = [UIImage imageNamed:@"icon_paly_big-0"];
    _videoImage.centerX = self.bacView.centerX;
    _videoImage.centerY = self.bacView.centerY-H(_videoImage)/2;
    [self.bacView addSubview:_videoImage];
    [self.bacView bringSubviewToFront:_videoImage];
    
}


/**
 *  评论点击事件
 */
-(void)ComentClick{
    
    [self.deleget CommentSelect:self.tid Row:2];
}

- (IBAction)pramisBtn:(UIButton *)sender {

    if ([self.app.isLogin isEqualToString:@"YES"]) {
        
        if (!sender.selected) {
           
                [[Manager sharedManager]RequestWhithURL:GetFormhash AndParameter:nil AndCompletionType:^(id responseObject, DIEError *error) {
                    if ([responseObject[@"status"] isEqualToString:@"SUCCESS"]) {
                        //        @{@"formhash":responseObject[@"data"][@"formhash"]}
                        [self pramisWhithFormHash:responseObject[@"data"][@"formhash"] AndButton:sender];
                    }
                }];
        }
        
    }else{
        
        [self.deleget PramisNoLogin];
    }
}


-(void)pramisWhithFormHash:(NSString *)formHash AndButton:(UIButton *)sender{

    [[Manager sharedManager]RequestWhithURL:[NSString stringWithFormat:AticlePramis,(long)_model.tid.integerValue,formHash] AndParameter:nil AndCompletionType:^(id responseObject, DIEError *error) {
        
        if (error) {
            if ([self.deleget respondsToSelector:@selector(PraimFile:)]) {
                [self.deleget PraimFile:@"点赞失败，请价差网络"];
            }
        }else{
            
            if ([responseObject[@"status"] isEqualToString:@"SUCCESS"]) {
                
                self.model.isZan = YES;
                sender.selected = YES;
                
                [UIView animateWithDuration:0.5 animations:^{
                    
                    sender.imageView.transform = CGAffineTransformScale(sender.imageView.transform, 0.65, 0.65);
                    
                } completion:^(BOOL finished) {
                    NSInteger count = sender.titleLabel.text.integerValue+1;
                    
                    [sender setTitle:[NSString stringWithFormat:@"%ld",(long)count] forState:UIControlStateNormal];
                }];
                
                
            }else{
            
                if ([self.deleget respondsToSelector:@selector(PraimFile:)]) {
                    [self.deleget PraimFile:responseObject[@"message"]];
                }
            }
        }
    }];
}

-(void)pramBackTap{
    
}

-(void)SpaceClick{
    [self.deleget UserSpaceClick:_model.authorid];
}

@end
